﻿using MySSO.Core.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Dapper;

namespace MySSO.Core.Repository
{
    public class AdminUserRepository : BaseRepository
    {
        //private readonly ICacheManager _cacheManager;
        //private readonly int AllAdminUserCACHE_TIME = 30;
        //private readonly string AllAdminUserCACHE_KEY = "AllAdminUser";

        public AdminUserRepository()
        {
            
        }
        /// <summary>
        /// 根据auid获取管理员实体
        /// </summary>
        /// <param name="auid">用户id</param>
        /// <returns></returns>
        public AdminUserEntity GetAdminUserEntityById(long auid)
        {
            AdminUserEntity adminUserEntity = null;
            string sql = @"SELECT [AUID],[email],[password] ,[name],[chn_name] ,[sex],[Auths],[JoinTime] ,[JoinIp],[login_num]
                              ,[lock_to_time],[today_login_error],[error_today] ,[LastActiveTime],[LastActiveIp],[Disable],[DisableTime],[PosID],[TeamID]
                              ,[WeakPassword],[Skype],[QQ],[phone],[BQQ_OpenID],[IsTrial],[CreateAUID],[UpdateAUID] 
                            FROM [Acadsoc.COM.CN].dbo.uc_AdminUser WITH(NOLOCK) where auid=@auid";
            using (var connection = GetConnection())
            {
                adminUserEntity = connection.QueryFirstOrDefault<AdminUserEntity>(sql, new { auid = auid });
            }
            return adminUserEntity;
        }

        /// <summary>
        /// 根据邮箱获取管理员实体
        /// </summary>
        /// <param name="email"></param>
        /// <returns></returns>
        public AdminUserEntity GetAdminUserEntityByEmail(string email)
        {
            AdminUserEntity adminUserEntity = null;
            string sql = @"SELECT [AUID],[email],[password] ,[name],[chn_name] ,[sex],[Auths],[JoinTime] ,[JoinIp],[login_num]
                              ,[lock_to_time],[today_login_error],[error_today] ,[LastActiveTime],[LastActiveIp],[Disable],[DisableTime],[PosID],[TeamID]
                              ,[WeakPassword],[Skype],[QQ],[phone],[BQQ_OpenID],[IsTrial],[CreateAUID],[UpdateAUID] 
                            FROM uc_AdminUser WITH(NOLOCK) where email=@email";
            using (var connection = GetConnection())
            {
                adminUserEntity = connection.QueryFirstOrDefault<AdminUserEntity>(sql, new { email = email });
            }
            return adminUserEntity;
        }

        public int UpdateAdminUserEntity(AdminUserEntity adminUserEntity)
        {
            int returncount = 0;
            if (adminUserEntity == null)
            {
                return returncount;
            }
            string sql = @"update uc_AdminUser SET [email] = @email,[password] = @password,[name] = @name,[chn_name] = @chn_name,[sex] = @sex
                        ,[Auths] = @Auths,[JoinTime] = @JoinTime,[JoinIp] = @JoinIp
                        ,[login_num] = @login_num,[lock_to_time] = @lock_to_time,[today_login_error] =@today_login_error,[error_today] = @error_today
                        ,[LastActiveTime] =@LastActiveTime,[LastActiveIp] = @LastActiveIp,[Disable] = @Disable,[DisableTime] =@DisableTime
                        ,[PosID] = @PosID,[TeamID] = @TeamID,[WeakPassword] = @WeakPassword
                        ,[Skype] = @Skype,[QQ] = @QQ,[phone] = @phone,[BQQ_OpenID] = @BQQ_OpenID,[IsTrial] = @IsTrial,[CreateAUID] = @CreateAUID
                        ,[UpdateAUID] = @UpdateAUID where auid=@auid";
            using (var connection = GetConnection())
            {
                returncount = connection.Execute(sql, adminUserEntity);
            }
            return returncount;
        }

        /// <summary>
        /// 根据管理员ID获取管理员信息
        /// </summary>
        /// <param name="auid">管理员Id数组</param>
        /// <param name="files">显示条件</param>
        /// <param name="tablename">查询表</param>
        /// <returns></returns>
        //public List<T> GetAdminUserByAUID<T>(long[] auid, string files = "*", string tablename = "uc_AdminUser with(nolock)")
        //{
        //    List<T> list = new List<T>();
        //    string sql = "select " + files + " from " + tablename + " where AUID in @Auid";
        //    using (var c = GetConnection())
        //    {
        //        list = c.Query<T>(sql, new { Auid = auid }).ToList();
        //    }
        //    return list;
        //}

        /// <summary>
        /// 根据条件查询管理员列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        //public IList<AdminUserEntity> GetAdminUsers(AdminUserQuery query)
        //{
        //    IList<AdminUserEntity> list = null;

        //    var pg = new PredicateGroup { Operator = GroupOperator.And, Predicates = new List<IPredicate>() };
        //    if (query.TeamID.HasValue)
        //    {
        //        pg.Predicates.Add(Predicates.Field<AdminUserEntity>(f => f.TeamID, Operator.Eq, query.TeamID.Value));
        //    }



        //    IList<ISort> listsort = new List<ISort>();
        //    if (query.OrderByType == "desc")
        //    {
        //        listsort.Add(new Sort { Ascending = false, PropertyName = query.OrderByName });
        //    }
        //    else
        //    {
        //        listsort.Add(new Sort { Ascending = true, PropertyName = query.OrderByName });
        //    }
        //    using (var connection = GetConnection())
        //    {
        //        connection.Open();

        //        list = connection.GetList<AdminUserEntity>(pg, listsort).ToList();

        //        connection.Close();
        //    }
        //    return list;
        //}

        /// <summary>
        /// 修改管理用户
        /// </summary>
        /// <param name="pars">修改内容</param>
        /// <param name="aUID"></param>
        /// <returns></returns>
        //public int Update(Dictionary<string, object> pars, long aUID)
        //{
        //    int r = 0;
        //    using (var c = GetConnection())
        //    {
        //        string sql = "update uc_AdminUser set ";
        //        string setfiles = "";
        //        foreach (var item in pars)
        //        {
        //            setfiles += "," + item.Key.ToString() + "='" + item.Value.ToString() + "' ";
        //        }
        //        sql = sql + setfiles.Substring(1) + " where AUID=" + aUID;
        //        r = c.Execute(sql);
        //    }
        //    return r;
        //}



        /// <summary>
        /// 获取用户所在的部门团队
        /// </summary>
        /// <param name="value"></param>
        /// <param name="unitOfWork"></param>
        /// <returns></returns>
        //public uc_Team GetTeamById(int TeamID, IUnitOfWork unitOfWork = null)
        //{
        //    uc_Team model = new uc_Team();
        //    string sql = "select [TID],[TeamName],[ParentID],[TeamPath],[TeamType],[SortID],[Disable],[BQQ_DeptID] from uc_Team where TID=@TeamID";
        //    if (unitOfWork != null)
        //    {
        //        model = unitOfWork.Connection.Query<uc_Team>(sql, new { TeamID = TeamID }, unitOfWork.Transaction).SingleOrDefault();
        //        return model;
        //    }
        //    using (var c = GetConnection())
        //    {
        //        model = c.Query<uc_Team>(sql, new { TeamID = TeamID }).SingleOrDefault();

        //    }
        //    return model;
        //}



        //public IList<AdminUserEntity> GetAllAdminUser()
        //{
        //    IList<AdminUserEntity> list = null;
        //    string sql = @"select * from uc_AdminUser with(nolock)";
        //    using (var connection = GetConnection())
        //    {
        //        list = connection.Query<AdminUserEntity>(sql).ToList();
        //    }
        //    return list;
        //}

        /// <summary>
        /// 从缓存中获取所有管理员
        /// </summary>
        /// <returns></returns>
        //public IList<AdminUserEntity> GetAllAdminUserByCache()
        //{
        //    IList<AdminUserEntity> list;

        //    list = _cacheManager.Get<IList<AdminUserEntity>>(AllAdminUserCACHE_KEY);

        //    if (list == null || list.Count == 0)
        //    {
        //        list = this.GetAllAdminUser();
        //        //设置缓存
        //        _cacheManager.Set(AllAdminUserCACHE_KEY, list, AllAdminUserCACHE_TIME);
        //    }

        //    return list;
        //}


    }
}
